package com.tsing.cedap.common.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.web.multipart.MultipartFile;

public class FileCheckUtil {

    public static Boolean checkImageSuffix(MultipartFile file) {
        boolean flag = false;
        String[] fileTypes = new String[]{".jpg", ".jpeg", ".png", ".gif", ".bmp"};
        if (StrUtil.isNotBlank(file.getOriginalFilename())) {
            String fileName = file.getOriginalFilename().toLowerCase();
            for (int i = 0, length = fileTypes.length; i < length; i++) {
                if (fileName.endsWith(fileTypes[i])) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    public static Boolean checkOfficeSuffix(MultipartFile file) {
        boolean flag = false;
        String[] fileTypes = new String[]{".docx", ".doc", ".pdf", ".xlsx", ".xls", ".pptx", ".ppt"};
        if (StrUtil.isNotBlank(file.getOriginalFilename())) {
            String fileName = file.getOriginalFilename().toLowerCase();
            for (int i = 0, length = fileTypes.length; i < length; i++) {
                if (fileName.endsWith(fileTypes[i])) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    public static Boolean checkVideoSuffix(MultipartFile file) {
        boolean flag = false;
        String[] fileTypes = new String[]{".mp4", ".rmvb", ".flv", ".swf", ".wmv", ".avi", ".mpg", ".mpeg", ".mov", ".ram", ".rm"};
        if (StrUtil.isNotBlank(file.getOriginalFilename())) {
            String fileName = file.getOriginalFilename().toLowerCase();
            for (int i = 0, length = fileTypes.length; i < length; i++) {
                if (fileName.endsWith(fileTypes[i])) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

}
